package com.exonline.commons.utils;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;


import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *Token工具类
 * @author YUKI
 * @date 2024/11/8
 */
public class TokenUtil {
    private static final String SECRET = "y6plziidr4a5prcw6zupgyihrd6o0id2as1238dfda1dafmbm";

    private TokenUtil(){};
    /**
     *  解析token的内容,存储到类中
     * @author YUKI
     * @date 2024/11/9
     */
//    private void setToken(String token) {
//        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
//         userId = (String) body.get("userId");
//         userName = (String) body.get("userId");
//    }
    /**
     *检测token
     * @author YUKI
     * @date 2024/11/8
     */
    public boolean isFailed(HttpServletRequest req) {
        try {
            String token =  req.getHeader("Authorization").substring("Bearer ".length());
            Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
            return false;
        }catch (Exception e){
            return true;
        }
        //验证token时效性
//        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
//        try{
//        long exp = (long) body.get("exp");
//        return exp - System.currentTimeMillis() > 0;
//        }catch (Exception e){
//            return true;
//        }
    }
    /**
     *创建token
     * @author YUKI
     * @date 2024/11/8
     */
    public static   String createToken(String id,String name ) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        Map<String,Object> header = new HashMap<>();
        Map<String,Object> params = new HashMap();

        //添加头
        header.put("alg", "HS256");
        header.put("typ", "JWT");
        //添加负载
        params.put("id", id);
        params.put("name", name);
        //生成token内容,设定两周内有效
        String token = Jwts.builder()
                .setHeader(header)
                .setClaims(params)
                .setExpiration(new Date(System.currentTimeMillis()+1000*60*60*24*7))
                .signWith(SignatureAlgorithm.HS256,SECRET)
                .compact();
        return token;
    }

    public static String getId(HttpServletRequest req) {
        String token =  req.getHeader("Authorization").substring("Bearer ".length());
        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
       return(String) body.get("id");
    }
    public static String getCid(String token) {
        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        return(String) body.get("cid");
    }
    public static String getSid(String token) {
        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        return(String) body.get("sid");
    }
    public static String getName(HttpServletRequest req) {
        String token =  req.getHeader("Authorization").substring("Bearer ".length());
        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        return(String) body.get("name");
    }

    public static  String createPayToken(String sid,String cid ) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        Map<String,Object> header = new HashMap<>();
        Map<String,Object> params = new HashMap();

        //添加头
        header.put("alg", "HS256");
        header.put("typ", "JWT");
        //添加负载
        params.put("sid", sid);
        params.put("cid", cid);
        //生成token内容,设定两周内有效
        String token = Jwts.builder()
                .setHeader(header)
                .setClaims(params)
                .setExpiration(new Date(System.currentTimeMillis()+1000*30*30))
                .signWith(SignatureAlgorithm.HS256,SECRET)
                .compact();
        return token;
    }
    /**
     *获取
     * @author YUKI
     * @date 2024/11/9
     */

//    private void setToken(String token) {
//        this.token = token;
//    }
}
